package cn.com.bluemoon.daps.common.druid;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jarod.Kong
 * @date 2020/11/11 11:42
 */
@Configuration
@ConditionalOnProperty(
        value = {"bm.dap.druid.enable"},
        matchIfMissing = true
)
public class DruidConfig {

    @Bean
    public ServletRegistrationBean<StatViewServlet> statViewServlet(){
        //druid监控页面的url
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<StatViewServlet>(new StatViewServlet(),"/dap/druid/*");
        StatViewServlet statViewServlet = new StatViewServlet();
        bean.setServlet(statViewServlet);
        Map<String,String> initParams = new HashMap<>(8);
        initParams.put("loginUsername","bm-druid");   //登陆用户名
        initParams.put("loginPassword","bm123@321");  //密码
        initParams.put("allow","");                //允许哪些ip
        initParams.put("deny","");                 //拒绝ip
        bean.setInitParameters(initParams);
        return bean;
    }
    //2.配置一个web监控的filter,监控sql
    @Bean
    public FilterRegistrationBean<WebStatFilter> webStatFilter(){
        FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new WebStatFilter());
        Map<String,String> initParams = new HashMap<>(4);
        initParams.put("exclusions","*.js,*.css,*.html,/druid/*");
        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}
